package com.bootcamp.servlet.cart;

/**
 * @program: wrok-1101
 * @author: Keyu Li
 * @create: 2021-11-05 20:22
 **/

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "LoginServlet", value = "/LoginServlet/*")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        PrintWriter writer = response.getWriter();
        Object userId = session.getAttribute("userId");
        Object logoff = request.getParameter("logoff");
        if (logoff != null && "1".equals(logoff) && userId != null) {
            session.invalidate();
            String storepage = request.getContextPath() + "/ShopStoreServlet";
            writer.println("退出登录成功！正在跳转购物页面ing");
            response.setHeader("refresh", "1;url=" + storepage);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();
        PrintWriter writer = response.getWriter();
        String userIdForm = (String) request.getParameter("userId");
        session.setAttribute("userId",Integer.parseInt(userIdForm));
        String storepage = request.getContextPath() + "/ShopStoreServlet";
        writer.println("登录成功！正在跳转购物页面ing");
        response.setHeader("refresh", "1;url=" + storepage);

    }
}
